package com.nbx.app.zeuscmdport;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.sax.Element;
import android.sax.EndElementListener;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.util.Xml;
import android.view.View;
import android.widget.AdapterView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class main extends ListActivity implements OnItemClickListener {

	private List<Map<String, Object>> mData;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        mData = parseDemosXml();

        SimpleAdapter adapter = new SimpleAdapter(this, mData, R.layout.item,
        		new String[] { "title", "description", "image" },
        		new int[] { R.id.item_title, R.id.item_description, R.id.item_image });
        setListAdapter(adapter);
        getListView().setOnItemClickListener(this);
    }

	private List<Map<String, Object>> parseDemosXml() {
    	
    	final List<Map<String, Object>> items = new ArrayList<Map<String, Object>>();
    	final Map<String, Object> currentItem = new HashMap<String, Object>();
    	
    	RootElement root = new RootElement("demos");
    	Element item = root.getChild("demo");
    	item.setEndElementListener(new EndElementListener() {
    		public void end() {
    			items.add(new HashMap<String, Object>(currentItem));
    		}
    	});
    	item.getChild("title").setEndTextElementListener(new EndTextElementListener() {
    		public void end(String value) {
    			currentItem.put("title", value);
    		}
    	});
    	item.getChild("description").setEndTextElementListener(new EndTextElementListener() {
    		public void end(String value) {
    			currentItem.put("description", value);
    		}
    	});
    	item.getChild("id").setEndTextElementListener(new EndTextElementListener() {
    		public void end(String value) {
    			currentItem.put("id", Integer.parseInt(value));
    		}
    	});
    	item.getChild("image").setEndTextElementListener(new EndTextElementListener() {
    		public void end(String value) {
    			int resId = getResources().getIdentifier(value, null, getPackageName());
    			currentItem.put("image", resId);
    		}
    	});
    	
    	try {
            Xml.parse(getResources().openRawResource(R.raw.demos),
            		Xml.Encoding.UTF_8, root.getContentHandler());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        
    	return items;
    }

	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Intent intent = new Intent();
		Bundle bundle = new Bundle();

		
		// TODO
		intent.setClass(this, GLSurfaceViewActivity.class);
		bundle.putInt("INDEX", arg2);
		bundle.putString("TITLE", mData.get(arg2).get("title").toString());
		intent.putExtras(bundle);
		startActivity(intent);
	}
}